/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mh.bol;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import mh.dal.DAO;
import mh.entity.EmployeeEn;

/**
 *
 * @author VIETNH
 */
@ManagedBean
@RequestScoped
public class ManageEmployeeBO {

    DAO dao = new DAO();

    public boolean checkLogin(String userName, String password) {
        Properties p = new Properties();
        p.put(1, userName);
        p.put(2, password);
        int count = 0;
        try {
            ResultSet rs = (ResultSet) dao.executeProc("sp_CheckLogin", p, "query");

            if (rs.next()) {
                count = rs.getInt(1);
                if(count > 0){
                    return true;
                }
                return false;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return true;
    }
    
    public ArrayList<EmployeeEn> getAllEmployee() {
        ArrayList<EmployeeEn> listEmp = new ArrayList<EmployeeEn>();
        Properties p = new Properties();
        try {
            ResultSet rs = (ResultSet) dao.executeProc("sp_GetAllEmployee", p, "query");

            while (rs.next()) {
                EmployeeEn emp = new EmployeeEn();
                emp.setEid(rs.getInt("e_id"));
                int type = rs.getInt("e_type");
                if(type == 1){
                    emp.setTypeString("Admin");
                } else if(type == 2){
                    emp.setTypeString("Technician");
                } else {
                    emp.setTypeString("Employee");
                }
                emp.setEtype(type);
                emp.setDid(rs.getInt("d_id"));
                emp.setDname(rs.getString("d_name"));
                emp.setUserName(rs.getString("e_username"));
                emp.setPassword(rs.getString("e_password"));
                emp.setFullName(rs.getString("e_fullname"));
                emp.setEmail(rs.getString("e_email"));
                emp.setPhone(rs.getString("e_phone"));
                emp.setGender(rs.getBoolean("e_gender"));
                emp.setImage(rs.getString("e_image"));
                emp.setDateJoin(rs.getString("e_datejoin"));
                emp.setActive(rs.getBoolean("e_active"));
                listEmp.add(emp);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return listEmp;
    }
    
    public boolean actionEmployee(String action,EmployeeEn emp){
        Properties p = new Properties();
        p.put(1, action);
        p.put(2, emp.getEid());
        p.put(3, emp.getEtype());
        p.put(4, emp.getDid());
        p.put(5, emp.getUserName());
        p.put(6, emp.getPassword());
        p.put(7, emp.getFullName());
        p.put(8, emp.getEmail());
        p.put(9, emp.getPhone());
        p.put(10, emp.isGender());
        p.put(11, emp.getImage());
        p.put(12, emp.getDateJoin());
        p.put(13, emp.isActive());

        return (Boolean) dao.executeProc("sp_Employee", p, "update");
    }
    
    public boolean deleteEmployee(String eid){
        Properties p = new Properties();
        p.put(1, eid);

        return (Boolean) dao.executeProc("sp_DeleteEmployee", p, "update");
    }
    
    public EmployeeEn viewEmpByUserName(String userName){
        EmployeeEn emp = null;
        Properties p = new Properties();
        p.put(1, userName);
        try {
            ResultSet rs = (ResultSet) dao.executeProc("sp_GetEmployeeByUserName", p, "query");

            if (rs.next()) {
                emp = new EmployeeEn();
                emp.setEid(rs.getInt("e_id"));
                emp.setEtype(rs.getInt("e_type"));
                emp.setDid(rs.getInt("d_id"));
                emp.setUserName(rs.getString("e_username"));
                emp.setPassword(rs.getString("e_password"));
                emp.setFullName(rs.getString("e_fullname"));
                emp.setEmail(rs.getString("e_email"));
                emp.setPhone(rs.getString("e_phone"));
                emp.setGender(rs.getBoolean("e_gender"));
                emp.setImage(rs.getString("e_image"));
                emp.setDateJoin(rs.getString("e_datejoin"));
                emp.setActive(rs.getBoolean("e_active"));
                return emp;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return emp;
    }
    
    public EmployeeEn viewDetailEmployee(String eid){
        EmployeeEn emp = null;
        Properties p = new Properties();
        p.put(1, eid);
        try {
            ResultSet rs = (ResultSet) dao.executeProc("sp_GetEmployeeByEID", p, "query");

            if (rs.next()) {
                emp = new EmployeeEn();
                emp.setEid(rs.getInt("e_id"));
                emp.setEtype(rs.getInt("e_type"));
                emp.setDid(rs.getInt("d_id"));
                emp.setDname(rs.getString("d_name"));
                emp.setUserName(rs.getString("e_username"));
                emp.setPassword(rs.getString("e_password"));
                emp.setFullName(rs.getString("e_fullname"));
                emp.setEmail(rs.getString("e_email"));
                emp.setPhone(rs.getString("e_phone"));
                emp.setGender(rs.getBoolean("e_gender"));
                emp.setImage(rs.getString("e_image"));
                emp.setDateJoin(rs.getString("e_datejoin"));
                emp.setActive(rs.getBoolean("e_active"));
                return emp;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return emp;
    }

    /** Creates a new instance of ManageEmployeeBO */
    public ManageEmployeeBO() {
    }
}
